package cn.springcloud.fix.demo.leecode;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicInteger;

@Slf4j
public class Test04 {

    private static volatile boolean a = false;
    private static volatile boolean b = false;

    public static void main(String[] args) throws InterruptedException {
        //test1();
        test2();
    }

    private static void test2() throws InterruptedException {
        AtomicInteger count = new AtomicInteger();
        MyRunnable2 a = new MyRunnable2("A", count);
        MyRunnable2 b = new MyRunnable2("B", count);
        MyRunnable2 c = new MyRunnable2("C", count);
        new Thread(a).start();
        new Thread(b).start();
        new Thread(c).start();
    }

    private static class MyRunnable2 implements Runnable {
        private final String name;
        private final AtomicInteger count;

        public MyRunnable2(String name, AtomicInteger count) {
            this.name = name;
            this.count = count;
        }

        @Override
        public void run() {
            int x = 0;
            while (x < 3) {
                if (count.get() % 3 == 0 && name.equals("A")) {
                    log.info("线程{}打印 A", Thread.currentThread().getName());
                    count.incrementAndGet();
                    x++;
                } else if (count.get() % 3 == 1 && name.equals("B")) {
                    log.info("线程{}打印 B", Thread.currentThread().getName());
                    count.incrementAndGet();
                    x++;
                } else if (count.get() % 3 == 2 && name.equals("C")) {
                    log.info("线程{}打印 C", Thread.currentThread().getName());
                    count.incrementAndGet();
                    x++;
                }
            }
        }
    }

    private static void test1() throws InterruptedException {
        Object a = new Object();
        Object b = new Object();
        Object c = new Object();
        MyRunnable pa = new MyRunnable("A", c, a);
        MyRunnable pb = new MyRunnable("B", a, b);
        MyRunnable pc = new MyRunnable("C", b, c);

        new Thread(pa).start();
        Thread.sleep(100);  //确保按顺序A、B、C执行
        new Thread(pb).start();
        Thread.sleep(100);
        new Thread(pc).start();
        Thread.sleep(100);
    }

    static class MyRunnable implements Runnable {

        private String name;
        private final Object prev;
        private final Object self;

        public MyRunnable(String name, Object prev, Object self) {
            this.name = name;
            this.prev = prev;
            this.self = self;
        }

        @Override
        public void run() {
            int count = 10;
            while (count > 0) {
                synchronized (prev) {
                    synchronized (self) {
                        System.out.print(name);
                        count--;
                        self.notify();
                    }
                    try {
                        prev.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
}
